package com.gxuwz.leave.servlet;

import com.gxuwz.leave.context.AppContext;
import com.gxuwz.leave.entity.Department;
import com.gxuwz.leave.service.DepService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet(name="depServlet", urlPatterns = {"/dep"})
public class DepServlet extends BaseHttpServlet{
    private final static String VIEW_ADD = "/WEB-INF/views/department/dep_add.jsp";

    private final static String VIEW_EDIT = "/WEB-INF/views/department/dep_edit.jsp";
    private final static String VIEW_LIST = "/WEB-INF/views/department/dep_list.jsp";
    private final static String ACTION_LIST = "/dep?action=list";
    private DepService depService;

    @Override
    public void init() throws ServletException {
        super.init();
        depService = AppContext.getBean(DepService.class);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String action = req.getParameter("action");

        String view = ErrorPage_500;
        try{
            switch (action){
                //导向新增页面
                case "input":
                    view = VIEW_ADD;
                    Department dep = new Department();
                    req.setAttribute("dep", dep);
                    break;
                //提交新增
                case "add":
                    dep = new Department();
                    dep.setDepID(req.getParameter("depID"));
                    dep.setDepName(req.getParameter("depName"));
                    depService.addDep(dep);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //编辑操作时，获取那一行的数据
                case "get":
                    String depID = req.getParameter("depID");
                    dep = depService.findDep(depID);
                    view = VIEW_EDIT;
                    req.setAttribute("dep", dep);
                    break;
                //提交编辑操作
                case "edit":
                    dep = new Department();
                    dep.setDepID(req.getParameter("depID"));
                    dep.setDepName(req.getParameter("depName"));
                    depService.editDep(dep);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //搜索
                case "search":
                    String keywords = req.getParameter("keywords");
                    dep = new Department();
                    dep.setDepID(keywords);
                    List<Department> list = depService.list(dep);
                    if(list.isEmpty()){
                        dep = new Department();
                        dep.setDepName(keywords);
                        list = depService.list(dep);
                    }
                    view = VIEW_LIST;
                    req.setAttribute("list", list);
                    break;
                //删除操作
                case "delete":
                    depID = req.getParameter("depID");
                    depService.deleteDep(depID);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                case "list":
                    List<Department> depList = depService.list(null);
                    view = VIEW_LIST;
                    req.setAttribute("list", depList);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            req.setAttribute("message", e.getMessage());
            req.setAttribute("operation", "failure");
            view = ACTION_LIST;
        }
        super.doDispatch(req,resp,view);
    }
}
